﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace vJine.Lua.GUI.UI {
    /// <summary>
    /// LuaOptions.xaml 的交互逻辑
    /// </summary>
    public partial class LuaOptions : UserControl {
        public LuaOptions() {
            InitializeComponent();
        }

        public LuaContext luaContext { get; set; }

        public bool IsResult { get; set; }

        public void set_bound(System.Collections.IEnumerable objData) {
            ValueTemplateSelector vts =
                this.FindResource("valueTemplateSelector") as ValueTemplateSelector;
            vts.IsResult = this.IsResult;

            this.dgvParams.ItemsSource = objData;
        }

        private void Single_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            e.Handled = true;

            ComboBox cmb = sender as ComboBox;
            if (cmb == null) {
                return;
            }

            NodeConfig nConfig = cmb.Tag as NodeConfig;
            if (nConfig == null) {
                return;
            }
            List<ListItem> lists = cmb.ItemsSource as List<ListItem>;
            if (lists == null) {
                return;
            }

            try {
                int sIndex = cmb.SelectedIndex;
                for (int i = 0, len = lists.Count; i < len; i++) {
                    lists[i].Checked = i == sIndex;
                }
                nConfig.trig_on_changed = cmb.IsMouseCaptured;
                nConfig.value = lists[sIndex].Value;
            } catch (Exception ex) {
                this.show_error(ex);
            } finally {
                nConfig.trig_on_changed = true;
            }
        }

        private void Multi_SelectionChanged(object sender, RoutedEventArgs e) {
            e.Handled = true;

            CheckBox chb = sender as CheckBox;
            if (chb == null) {
                return;
            }
            ComboBox cmb = chb.Tag as ComboBox;
            if (cmb == null) {
                return;
            }

            NodeConfig nConfig = cmb.Tag as NodeConfig;
            if (nConfig == null) {
                return;
            }
            if (nConfig.list == null || nConfig.list.Count == 0) {
                return;
            }

            List<object> list_args = new List<object>();
            for (int i = 0, len = nConfig.list.Count; i < len; i++) {
                ListItem config_i_list_j = nConfig.list[i];
                if (!config_i_list_j.Checked) {
                    continue;
                }
                list_args.Add(config_i_list_j.Value);
            }
            try {
                nConfig.trig_on_changed = cmb.IsMouseCaptured;
                nConfig.value = list_args;
            } catch (Exception ex) {
                this.show_error(ex);
            } finally {
                nConfig.trig_on_changed = true;
            }
        }

        private void dgvParams_MouseDoubleClick(object sender, MouseButtonEventArgs e) {
            DependencyObject target =
                this.dgvParams.InputHitTest(e.GetPosition(this.dgvParams)) as DependencyObject;

            while (target != null) {
                if (target is DataGridCell && ((DataGridCell)target).Column != null) {
                    DataGridCell cell = (DataGridCell)target;
                    int column = cell.Column.DisplayIndex;
                    if (column < 0 || column > 1) {
                        break;
                    }
                } else if (target is DataGridRow) {
                    NodeConfig config = (NodeConfig)((DataGridRow)target).Item;
                    if (string.IsNullOrEmpty(config.cmd_dbclick)) {
                        return;
                    }

                    try {
                        config.value = config.exec(this.luaContext, config.cmd_dbclick);
                    } catch (Exception ex) {
                        this.show_error(ex);
                    }
                    return;
                }

                target = VisualTreeHelper.GetParent(target);
            }

        }

        void show_error(Exception ex) {
            if (ex == vJine.Core.CoreException.Cancel) {
                if (this.luaContext.is_function("on_warn")) {
                    this.luaContext.exec("on_warn", "[运行取消]");
                }
                return;
            }

            if (ex.InnerException != null) {
                ex = ex.InnerException;
            }
            if (this.luaContext.is_function("on_error")) {
                this.luaContext.exec("on_error", ex.Message);
            } else {
                MessageBox.Show(
                    ex.Message, "运行错误",
                    MessageBoxButton.OK, MessageBoxImage.Stop);
            }
        }

        private void dgvParams_SizeChanged(object sender, SizeChangedEventArgs e) {

        }
    }
}
